package com.bsoft.gol.hcs.clinical.dao;

import com.bsoft.gol.hcs.clinical.bean.qo.QueryMedicalRecordTypeListQo;
import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.persistence.support.hibernate.template.AbstractHibernateStatelessResultAction;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.persistence.support.hibernate.template.HibernateStatelessResultAction;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.SQLQuery;
import org.hibernate.StatelessSession;
import org.hibernate.transform.Transformers;
import pcn.consultation.MedicalRecordType;
import com.bsoft.gol.hcs.utils.CommonUtils;

import java.math.BigInteger;
import java.util.List;
import java.util.Map;

/**
 * Created by Sangs on 2020/4/3.
 */
public abstract class MedicalRecordTypeDAO extends ctd.persistence.support.hibernate.HibernateSupportDelegateDAO<MedicalRecordType> {

    public MedicalRecordTypeDAO() {
        this.setKeyField("id");
    }

    @DAOMethod(sql = "from MedicalRecordType where id =:id and status!=2")
    public abstract MedicalRecordType getBaseMedicalRecordTypeById(@DAOParam("id") String id);


    private String createMedicalRecordTypeListSql(QueryMedicalRecordTypeListQo qo, boolean isQueryList, String type){
        StringBuffer hsql = new StringBuffer();
        if(isQueryList){
            hsql.append("select id,medicalRecordType_no medicalRecordTypeNo,medicalRecordType_name medicalRecordTypeName,status ");
        }else {
            hsql.append("select  count(1) ");
        }
        if (CommonUtils.isEmpty(qo.getDeptIdList())&&type=="all") {
            hsql.append(" from base_MedicalRecordType a   where a.status !='2'  ");
        }else if(type=="all"){
            hsql.append(" from base_MedicalRecordType a left join base_medicalRecordType_dept b on a.id = b.medicalRecordTypeId  where a.status !='2'  ");
        }
        if (CommonUtils.isEmpty(qo.getDeptIdList())&&type=="use") {
            hsql.append(" from base_MedicalRecordType a   where a.status ='1'  ");
        }else if(type=="use"){
            hsql.append(" from base_MedicalRecordType a left join base_medicalRecordType_dept b on a.id = b.medicalRecordTypeId  where a.status ='1'  ");
        }
        if (qo.getObjectId() != null && StringUtils.isNotEmpty(qo.getObjectId())) {
            hsql.append(" and a.object_id = '" + qo.getObjectId() + "'");
        }
        if (qo.getMedicalRecordTypeName() != null && StringUtils.isNotEmpty(qo.getMedicalRecordTypeName())) {
            hsql.append(" and a.medicalRecordType_name like '" + qo.getMedicalRecordTypeName() + "%'");
        }
        if (CommonUtils.isNotEmpty(qo.getDeptIdList())) {
            hsql.append(" and b.deptId in(:deptIdList)");
        }
        return hsql.toString();
    }

    /**
     *
     *
     * @return
     */
    public List<Map<String, Object>> queryMedicalRecordTypeList(final QueryMedicalRecordTypeListQo qo, final String type) {
        final HibernateStatelessResultAction<List<Map<String, Object>>> action = new AbstractHibernateStatelessResultAction<List<Map<String, Object>>>() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                String sql = "";
                if(type=="all"){
                 sql = createMedicalRecordTypeListSql(qo,true,"all");
                }else if(type=="use"){
                    sql = createMedicalRecordTypeListSql(qo,true,"use");
                }
                SQLQuery sqlQuery = ss.createSQLQuery(sql);
                if (qo.getDeptIdList() != null && CommonUtils.isNotEmpty(qo.getDeptIdList())) {
                    sqlQuery.setParameterList("deptIdList",qo.getDeptIdList());
                }
                sqlQuery = (SQLQuery) sqlQuery.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
                //分页
                int first = qo.getPageNo() > 0 ? (qo.getPageNo() - 1) * qo.getPageSize() : 0;
                sqlQuery.setFirstResult(first);
                sqlQuery.setMaxResults(qo.getPageSize());

                List<Map<String, Object>> ls = (List<Map<String, Object>>) sqlQuery.list();
                setResult(ls);
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }


    /**
     * 查询所有条数
     * @return
     */
    public BigInteger queryMedicalRecordTypeListCount(final QueryMedicalRecordTypeListQo qo, final String type) {
        HibernateStatelessResultAction<BigInteger> action = new AbstractHibernateStatelessResultAction<BigInteger>() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                String sql = createMedicalRecordTypeListSql(qo,false,type);
                SQLQuery sqlQuery = ss.createSQLQuery(sql);
                if (CommonUtils.isNotEmpty(qo.getDeptIdList())) {
                    sqlQuery.setParameterList("deptIdList",qo.getDeptIdList());
                }
                List list = sqlQuery.list();
                if (list.size() == 1) {
                    Object o = list.get(0);
                    BigInteger count = (BigInteger) o;
                    setResult(count);
                }
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }


    @DAOMethod(sql = "update MedicalRecordType set status=:status where id =:id ")
    public abstract void updateMedicalRecordTypeStatus(@DAOParam("id") String id,@DAOParam("status") String status);


}